package org.lgiavedoni.android.franq.cron;

import java.util.Calendar;

import org.lgiavedoni.android.franq.core.cron.CronTask;
import org.lgiavedoni.android.franq.core.cron.RepetibleCron;
import org.lgiavedoni.android.franq.service.communication.CommunicationService;
import org.lgiavedoni.android.franq.service.sync.SyncService;
import org.lgiavedoni.android.franq.util.date.DateUtil;

public class SyncCron extends CronTask implements RepetibleCron {

	@Override
	public void run() {
		SyncService.inst.exec();
		CommunicationService.inst.sendDefaultTextMessage("Synchronization done for today");
	}

	@Override
	public long getDelay() {
		// Starting today at 10:07:00
		Calendar c = Calendar.getInstance();
		c.set(Calendar.HOUR_OF_DAY, 22);
		c.set(Calendar.MINUTE, 07);
		c.set(Calendar.SECOND, 0);
		if ((c.getTimeInMillis() - DateUtil.getTimeInMillis()) < 0) {
			c.set(Calendar.DAY_OF_YEAR, c.get(Calendar.DAY_OF_YEAR) + 1);
		}
		return c.getTimeInMillis() - DateUtil.getTimeInMillis();
	}

	@Override
	public long getPeriod() {
		// every 24 hours
		return DateUtil.getMillisecondsOfHours(24);
	}

}
